﻿using AutomatedTestDataManagement.Models;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;

namespace AutomatedTestDataManagement.Controllers
{
    public class SitesPageController : Controller
    {
        private ApplicationTestDataEntities db = new ApplicationTestDataEntities();

        //
        // GET: /SitesPage/

        public ActionResult Index()
        {
            var sitespages = db.SitesPages.Include(s => s.Page).Include(s => s.Site);
            return View(sitespages.ToList());
        }

        //
        // GET: /SitesPage/Details/5

        public ActionResult Details(int id = 0)
        {
            SitesPage sitespage = db.SitesPages.Find(id);
            if (sitespage == null)
            {
                return HttpNotFound();
            }
            return View(sitespage);
        }

        //
        // GET: /SitesPage/Create

        public ActionResult Create(int dataid = 0)
        {
            ViewBag.PageID = new SelectList(db.Pages, "PageID", "PageName");
            
            if (dataid == 0) 
            {
                ViewBag.SiteID = new SelectList(db.Sites, "DataID", "SiteName");
            }
            else
            {
                ViewBag.SiteID = new SelectList(db.Sites, "DataID", "SiteName", dataid);
                ViewBag.SelectedSiteID = dataid;
            }
            return View();
        }

        //
        // POST: /SitesPage/Create

        [HttpPost]
        public ActionResult Create(SitesPage sitespage)
        {
            if (ModelState.IsValid)
            {
                db.SitesPages.Add(sitespage);
                db.SaveChanges();
                if (Request.Form["SelectedSiteID"] != null)
                {
                    return RedirectToAction("Edit", "Site", new { id = Request.Form["SelectedSiteID"] });
                }
                return RedirectToAction("Index");
            }

            if (Request.Form["SelectedSiteID"] != null)
            {
                ViewBag.SelectedSiteID = Request.Form["SelectedSiteID"];
            }
            ViewBag.PageID = new SelectList(db.Pages, "PageID", "PageName", sitespage.PageID);
            ViewBag.SiteID = new SelectList(db.Sites, "DataID", "SiteName", sitespage.SiteID);
            return View(sitespage);
        }

        //
        // GET: /SitesPage/Edit/5

        public ActionResult Edit(int id = 0)
        {
            SitesPage sitespage = db.SitesPages.Find(id);
            if (sitespage == null)
            {
                return HttpNotFound();
            }
            ViewBag.PageID = new SelectList(db.Pages, "PageID", "PageName", sitespage.PageID);
            ViewBag.SiteID = new SelectList(db.Sites, "DataID", "SiteName", sitespage.SiteID);
            return View(sitespage);
        }

        //
        // POST: /SitesPage/Edit/5

        [HttpPost]
        public ActionResult Edit(SitesPage sitespage)
        {
            if (ModelState.IsValid)
            {
                db.Entry(sitespage).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PageID = new SelectList(db.Pages, "PageID", "PageName", sitespage.PageID);
            ViewBag.SiteID = new SelectList(db.Sites, "DataID", "SiteName", sitespage.SiteID);
            return View(sitespage);
        }

        //
        // GET: /SitesPage/Delete/5

        public ActionResult Delete(int id = 0, int siteID = 0, int pageID = 0, string returnToView = "Edit")
        {
            SitesPage sitespage = db.SitesPages.Find(id);
            if (sitespage == null)
            {
                return HttpNotFound();
            }

            if (siteID != 0)
            {
                ViewBag.SelectedSiteID = siteID;
            }
            if (pageID != 0)
            {
                ViewBag.PageID = pageID;
            }
            ViewBag.returnToView = returnToView;

            return View(sitespage);
        }

        //
        // POST: /SitesPage/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var pageID = Request.Form["PageID"];
            var selSiteID = Request.Form["SelectedSiteID"];
            var returnToAction = Request.Form["ReturnToAction"];
            
            SitesPage sitespage = db.SitesPages.Find(id);
            db.SitesPages.Remove(sitespage);
            db.SaveChanges();

            if (Request.Form["SelectedSiteID"] != null)
            {
                return RedirectToAction(returnToAction, "Site", new { id = Request.Form["SelectedSiteID"] });
            }
            else
            {
                if (Request.Form["PageID"] != null)
                {
                    return RedirectToAction("Delete", "Page", new { id = pageID });
                }
                else
                {
                    return RedirectToAction(returnToAction, "Site", new { id = sitespage.SiteID });
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}